package org.polaris.authority.service.impl;

import org.polaris.authority.domain.Role;
import org.polaris.authority.dto.RolePermissionDTO;
import org.polaris.authority.dto.SetRoleDTO;
import org.polaris.authority.mapper.RoleMapper;
import org.polaris.authority.query.RoleQuery;
import org.polaris.authority.service.IRoleService;
import org.polaris.basic.service.impl.BaseServiceImpl;
import org.polaris.basic.tool.PageResult;
import org.polaris.org.domain.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

@Service
@Primary
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;


    @Override
    public Role selectBySn(String sn) {
        return roleMapper.selectBySn(sn);
    }




    @Override
    public void setRolePermission(RolePermissionDTO dto) {
        // 1 根据roleId先删除这个角色原来绑定的权限
        roleMapper.deleteByRoleId(dto.getRoleId());

        // 2 新增角色和权限的关联关系
        // 注意:1个角色是可以有多个权限的,怎么写?
        // 方式一: 循环dto.permissionSns 每次循环,都调用一次insert(), 不建议使用,减少操作数据库
        // 方案二: 批量新增Available
        roleMapper.saveRolePermission(dto.getRoleId(),dto.getPermissionSns());
    }

    @Override
    public List<String> getRolePermissionSns(Long roleId) {
        return roleMapper.getRolePermissionSns(roleId);
    }

    @Override
    public void setRoles(SetRoleDTO dto) {
        roleMapper.deleteRolesByEmpId(dto.getEid());
        roleMapper.saveRoles(dto.getEid(),dto.getRoleIds());
    }

    @Override
    public List<Long> getEmpRoles(Long eId) {
        return roleMapper.getEmpRoles(eId);

    }
}
